home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / GUSI / include / TFileSpec.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-08  |  5.8 KB  |  210 lines  |  [TEXT/MPS ]

  1. /*********************************************************************
  2. Project    :    GUSI                -    Grand Unified Socket Interface
  3. File        :    TFileSpec.h        -    C and C++ routines to deal with path names
  4. Author    :    Matthias Neeracher
  5. Started    :    06Jun92                                Language    :    MPW C/C++
  6. Modified    :    08Sep92    MN    Permission flags were the wrong way around
  7.                 12Sep92    MN    Renamed
  8.                 15Nov92    MN    Forgot a few consts
  9.                 15Nov92    MN    Renamed once again
  10.                 15Nov92    MN    Made suitable for use from C programs
  11.                 08Dec92    MN    Made Default() public
  12.                 03Jan93    MN    Prevent multiple includes
  13.                 15Jan93    MN    IsParentOf, operator ==
  14.                 06Feb93    MN    DefaultDir()
  15.                 14Mar93    MN    1.1.0 Baseline
  16.                 17Jul93    MN    TFileSpec::LastInfo()
  17.                 27Sep93    MN    FSpSmartMove(), Special constructors
  18. Last        :    27Sep93
  19. *********************************************************************/
  20.  
  21. #ifndef _TFILESPEC_
  22. #define _TFILESPEC_
  23.  
  24. /* These routines run both under System 7 and under older systems. */
  25.  
  26. #include <Files.h>
  27. #include <Folders.h>
  28.  
  29. #define kTempFileType    'TMPF'
  30.  
  31. #ifdef __cplusplus
  32.  
  33. /************************** The C++ only interface ***************************/
  34.  
  35. class TFileSpec : public FSSpec {
  36.     static OSErr        error;
  37.     static short        curVol;
  38.     static long            curDir;
  39.     static CInfoPBRec    lastInfo;
  40.  
  41.     OSErr    DefaultDir();
  42. public:
  43.     // Return last error
  44.     static OSErr        Error()                            {    return error;        }
  45.  
  46.     // Return last info. Valid after [], Exists
  47.     static const CInfoPBRec * LastInfo()            {    return &lastInfo;    }
  48.     
  49.     // Change current directory
  50.     static OSErr        ChDir(const TFileSpec & spec);
  51.  
  52.     // Set to current directory
  53.     OSErr    Default();
  54.  
  55.     TFileSpec()                                                                {}
  56.  
  57.     // Construct from FSSpec
  58.     TFileSpec(const FSSpec & spec, Boolean useAlias = false);
  59.  
  60.     // Construct from components
  61.     TFileSpec(short vRefNum, long parID, ConstStr31Param name, Boolean useAlias = false);
  62.  
  63.     // Construct from working directory & file name
  64.     TFileSpec(short wd, ConstStr31Param name, Boolean useAlias = false);
  65.  
  66.     // Construct from full or relative path
  67.     TFileSpec(const char * path, Boolean useAlias = false);
  68.  
  69.     // Construct using FindFolder()    
  70.     TFileSpec(OSType object, short vol = kOnSystemDisk, long dir = 0);
  71.     
  72.     // This is currently an expensive no-op, but if you call this before passing
  73.     // the FSSpec to a FSp routine, you are guaranteed to remain compatible forever.
  74.     void Bless();
  75.  
  76.     // Give full pathname
  77.     char *    FullPath() const;
  78.  
  79.     // Give path relative to current directory (as defined by HSetVol())
  80.     char *    RelPath() const;
  81.  
  82.     // Give information about the current object. If dirInfo is true, give information
  83.     //   about the current object's directory.
  84.     OSErr        CatInfo(CInfoPBRec & info, Boolean dirInfo = false) const;
  85.  
  86.     // If object is an alias file, resolve it. If gently is true, nonexisting files
  87.     //   are tolerated.
  88.     OSErr        Resolve(Boolean gently = true);
  89.  
  90.     // Resolve an existing object for which we already have a CInfoPBRec
  91.     OSErr        Resolve(const CInfoPBRec & info);
  92.  
  93.     // true if object exists
  94.     Boolean    Exists() const;
  95.  
  96.     // true if object is a parent directory of other
  97.     Boolean    IsParentOf(const TFileSpec & other) const;
  98.  
  99.     // Replace object with its parent directory
  100.     TFileSpec     operator--();
  101.  
  102.     // Equivalent to calling -- levels times
  103.     TFileSpec     operator-=(int levels);
  104.  
  105.     // Equivalent to calling -= on a *copy* of the current object
  106.     TFileSpec    operator-(int levels) const;
  107.  
  108.     // Replace directory object by object with given name inside the directory
  109.     TFileSpec     operator+=(ConstStr31Param name);
  110.     TFileSpec    operator+=(const char * name);
  111.  
  112.     // Non-destructive version of +=
  113.     TFileSpec    operator+(ConstStr31Param name) const;
  114.     TFileSpec    operator+(const char * name) const;
  115.  
  116.     // Return the index-th object in the *parent* directory of the current object
  117.     TFileSpec     operator[](short index) const;
  118.  
  119.     // Return if the two filespecs (not) are equal
  120.     Boolean        operator==(const TFileSpec & other) const;
  121.     Boolean        operator!=(const TFileSpec & other) const;
  122. };
  123.  
  124. inline Boolean IsFile(const CInfoPBRec & info)
  125. {
  126.     return !(info.dirInfo.ioFlAttrib & 0x10);
  127. }
  128.  
  129. inline Boolean IsAlias(const CInfoPBRec & info)
  130. {
  131.     return
  132.         !(info.hFileInfo.ioFlAttrib & 0x10) &&
  133.         (info.hFileInfo.ioFlFndrInfo.fdFlags & (1 << 15));
  134. }
  135.  
  136. inline Boolean DirIsExported(const CInfoPBRec & info)
  137. {
  138.     return (info.hFileInfo.ioFlAttrib & 0x20);
  139. }
  140.  
  141. inline Boolean DirIsMounted(const CInfoPBRec & info)
  142. {
  143.     return (info.hFileInfo.ioFlAttrib & 0x08);
  144. }
  145.  
  146. inline Boolean DirIsShared(const CInfoPBRec & info)
  147. {
  148.     return (info.hFileInfo.ioFlAttrib & 0x04);
  149. }
  150.  
  151. inline Boolean HasRdPerm(const CInfoPBRec & info)
  152. {
  153. #ifdef OnceThisFieldIsDefined
  154.     return !(info.dirInfo.ioACUser & 0x02);
  155. #else
  156.     return !(info.dirInfo.filler2 & 0x02);
  157. #endif
  158. }
  159.  
  160. inline Boolean HasWrPerm(const CInfoPBRec & info)
  161. {
  162. #ifdef OnceThisFieldIsDefined
  163.     return !(info.dirInfo.ioACUser & 0x04);
  164. #else
  165.     return !(info.dirInfo.filler2 & 0x04);
  166. #endif
  167. }
  168.  
  169. extern "C" {
  170. #endif
  171.  
  172. /* Routines shared between C and C++ */
  173.  
  174. /* Convert a working directory & file name into a FSSpec. */
  175. OSErr WD2FSSpec(short wd, ConstStr31Param name, FSSpec * desc);
  176.  
  177. /* Convert a FSSpec into a full pathname. */
  178. char * FSp2FullPath(const FSSpec * desc);
  179.  
  180. /* Works like FSp2FullPath, but creates a *relative* pathname if the object
  181.    is contained in the current directory.
  182. */
  183. char * FSp2RelPath(const FSSpec * desc);
  184.  
  185. /* Call GetCatInfo for file system object. */
  186. OSErr    FSpCatInfo(const FSSpec * desc, CInfoPBRec * info);
  187.  
  188. /* Return FSSpec of (vRefNum, parID) */
  189. OSErr FSpUp(FSSpec * desc);
  190.  
  191. /* Return FSSpec of file in directory denoted by desc */
  192. OSErr FSpDown(FSSpec * desc, ConstStr31Param name);
  193.  
  194. /* Return FSSpec of nth file in directory denoted by (vRefNum, parID) */
  195. OSErr FSpIndex(FSSpec * desc, short n);
  196.  
  197. /* Convert a pathname into a file spec. */
  198. OSErr Path2FSSpec(const char * path, FSSpec * desc);
  199.  
  200. /* Convert a special object into a file spec. */
  201. OSErr Special2FSSpec(OSType object, short vol, long dirID, FSSpec * desc);
  202.  
  203. /* Move & Rename File */
  204. OSErr FSpSmartMove(const FSSpec * from, const FSSpec * to);
  205.  
  206. #ifdef __cplusplus
  207. }
  208. #endif
  209. #endif
  210.